const http = require("http");
const fs = require('fs');
const path = require("path");
const { URL } = require("url");

function sendPage(req, res) {
    let filePath = path.join(__dirname , "post.html");
    let fileContent = fs.readFileSync(filePath);
    res.end(fileContent);
}

function postData(req, res){
    let urlObj = new URL("http://192.168.42.166:3000");
    console.log(urlObj);
    let str = '';
    req.on("data" , (chunk) => {
        str += chunk;
    });
    // 对于这个req.end就是说此时服务端已经完全接收了传送过来的数据
    req.on("end" , () => {
        console.log(str);
        res.end(str);
    });
}


http.createServer((req , res) => {
    switch(req.method) {
        case "GET":
            sendPage(req, res);
            break;
        case "POST":
            postData(req, res);
            break;
    }
}).listen(3000);